package com.dts.yjnb.service.service.impl;

import com.dts.common.core.page.TableDataInfo;
import com.dts.yjnb.service.domain.Agricultural;
import com.dts.yjnb.service.domain.CreditInfo;
import com.dts.yjnb.service.domainDto.CreditInfoDTO;
import com.dts.yjnb.service.service.ICreditInfoService;
import com.dts.yjnb.service.mapper.CreditInfoMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author XianXing
* @description 针对表【credit_info(信贷信息表)】的数据库操作Service实现
* @createDate 2025-10-30 11:08:18
*/
@Service
@Slf4j
public class CreditInfoServiceImpl implements ICreditInfoService{

    @Autowired
    private CreditInfoMapper creditInfoMapper;
    /**
     * 分页查询信贷信息
     */
    @Override
    public TableDataInfo pageQuery(CreditInfoDTO creditInfoDTO) {
        //属性拷贝
        CreditInfo creditInfo = new CreditInfo();
        BeanUtils.copyProperties(creditInfoDTO,creditInfo);

        // 开始分页
        PageHelper.startPage(creditInfoDTO.getPage(), creditInfoDTO.getPageSize());
        List<CreditInfo> list = creditInfoMapper.pageQuery(creditInfoDTO);
        // 将List转换为Page对象以获取分页信息
        Page<CreditInfo> page = (Page<CreditInfo>) list;

        long total = page.getTotal();
        List<CreditInfo> records = page.getResult();

        // 封装返回结果
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(total);
        tableDataInfo.setRows(records);

        return tableDataInfo;
    }

    /**
     * 根据ID获取信贷信息
     */
    @Override
    public CreditInfoDTO getByid(Long id) {
        CreditInfo creditInfo = creditInfoMapper.getById(id);
        CreditInfoDTO creditInfoDTO = new CreditInfoDTO();
        if (creditInfo != null) {
            BeanUtils.copyProperties(creditInfo, creditInfoDTO);
        }
        return creditInfoDTO;
    }

    /**
     * 新增信贷信息
     */
    @Override
    public boolean add(CreditInfoDTO creditInfoDTO) {
        CreditInfo creditInfo = new CreditInfo();
        BeanUtils.copyProperties(creditInfoDTO, creditInfo);
        return creditInfoMapper.add(creditInfo) > 0;
    }

    /**
     * 修改信贷信息
     */
    @Override
    public boolean update(CreditInfoDTO creditInfoDTO) {
        CreditInfo creditInfo = new CreditInfo();
        BeanUtils.copyProperties(creditInfoDTO, creditInfo);
        return creditInfoMapper.update(creditInfo) > 0;
    }

    /**
     * 删除信贷信息
     */
    @Override
    public boolean delete(Long id) {
        return creditInfoMapper.delete(id) > 0;
    }

    /**
     * 批量删除信贷信息
     */
    @Override
    public void deleteBatch(Long[] ids) {
        creditInfoMapper.deleteBatch(ids);
    }

    /**
     * 搜索信贷信息
     */
    @Override
    public List<CreditInfo> search(CreditInfoDTO creditInfoDTO) {
        List<CreditInfo> creditInfos = creditInfoMapper.search(creditInfoDTO);
        List<CreditInfoDTO> resultList = new ArrayList<>();
        for (CreditInfo creditInfo : creditInfos) {
            CreditInfoDTO dto = new CreditInfoDTO();
            BeanUtils.copyProperties(creditInfo, dto);
            resultList.add(dto);
        }

        Page<CreditInfo> creditInfoPage = new Page<>();
        return creditInfoPage;
    }

    /**
     * 批量修改状态
     */
    @Override
    public void updateStatus(Long[] ids, Integer status) {
        creditInfoMapper.updateStatus(ids, status);
    }

    /**
     * 批量修改推荐状态
     */
    @Override
    public void updateRecommend(Long[] ids, Integer isRecommend) {
        creditInfoMapper.updateRecommend(ids, isRecommend);
    }
}




